package P7.T04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class JMyFrame extends JFrame {
    private JPanel pnl=null;
    public JMyFrame() {
        this.setTitle("My Frame");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,500);
        JButton btn=new JButton("添加走马灯");
        add(btn, BorderLayout.NORTH);
        pnl=new JPanel();
        pnl.setLayout(null);
        pnl.setVisible(true);
        add(pnl, BorderLayout.CENTER);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random r=new Random();
                JLabel lbl=new JLabel();
                lbl.setBounds(r.nextInt(400),r.nextInt(400),150,20);
                lbl.setText("hello");
                lbl.setVisible(true);
                pnl.add(lbl);
                repaint();
                Thread t=new Thread(new Runnable() {
                    private String s="新年快乐          ";
                    @Override
                    public void run() {
                        while(true){
                            s=s.substring(1,s.length())+s.substring(0,1);
                            lbl.setText(s);
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }
                });
                t.start();
            }
        });
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        setVisible(true);
    }
}
